package org.forwhat.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: ForWhat
 * @package: org.forwhat.util
 * @className: DataUtilities
 * @author: LGJ
 * @description: TODO
 * @date: 2023/11/24 15:07
 * @version: 1.0
 */
public class DataUtilities extends ObjectMapper {
    private static ObjectMapper objectMapper = new ObjectMapper();

    public static String toJson(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public <T> T jsonToBean(String json, Class<T> clazz) {
        try {
            T t = objectMapper.readValue(json, clazz);
            return t;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

//    public static <T> List<Object> beanToObject(T object, Class<T> clazz, boolean hasNull) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
//        ArrayList<Object> objects = new ArrayList<>();
//        Field[] fields = clazz.getDeclaredFields();
//        for (Field field : fields) {
//            String name = field.getName();
//            String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
//            Method method = clazz.getDeclaredMethod(methodName, null);
//            Object fieldObject = method.invoke(object, null);
//            if (!hasNull) {
//                if (fieldObject != null) {
//                    objects.add(fieldObject);
//                    continue;
//                }
//                continue;
//            }
//            objects.add(fieldObject);
//
//
//        }
//
//        return objects;
//    }

}
